//
//  LZMyRestaurantInfoTextFieldTableViewCell.m
//  laziz_Merchant
//
//  Created by ZhaoDongBo on 2016/12/19.
//  Copyright © 2016年 XBN. All rights reserved.
//

#import "LZMyRestaurantInfoTextFieldTableViewCell.h"
#import "Masonry.h"

#import "LZCouponListModel.h"
#import "LZMerchantInfoModel.h"
#import "LZAlertView.h"

@interface LZMyRestaurantInfoTextFieldTableViewCell() <UITextFieldDelegate>

@property (nonatomic, strong) UILabel               * titleLabel;
@property (nonatomic, strong) UITextField           * textField;
@property (nonatomic, strong) UILabel               * subLabel;
@property (nonatomic, strong) UIView                * line;

@end

@implementation LZMyRestaurantInfoTextFieldTableViewCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self)
    {
        self.backgroundColor = UIColorFromRGB(0xffffff, 1);
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self configView];
        [self addAutoLayoutToCell];
    }
    return self;
}

- (void)callTextFieldBecomeFirstResponder
{
    [self.textField becomeFirstResponder];
}

- (void)configView {
    [self.contentView addSubview:self.titleLabel];
    [self.contentView addSubview:self.textField];
    [self.contentView addSubview:self.subLabel];
    [self.contentView addSubview:self.line];
}

- (void)addAutoLayoutToCell {
    
    WS(weakSelf);
    
    [self.titleLabel makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(10);
    }];
    
    [self.textField makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.leading.mas_equalTo(weakSelf.titleLabel.mas_trailing).with.offset(10);
    }];
    
    [self.subLabel makeConstraints:^(MASConstraintMaker *make) {
        make.leading.equalTo(self.textField.mas_trailing);
        make.centerY.mas_equalTo(weakSelf.centerY);
        make.trailing.mas_lessThanOrEqualTo(-10);
    }];

    [self.line makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.mas_equalTo(0);
        make.leading.mas_equalTo(10);
        make.trailing.mas_equalTo(-10);
        make.height.mas_equalTo(0.6);
    }];
}

- (UILabel *)titleLabel {
    
    if (_titleLabel == nil) {
        _titleLabel = [[UILabel alloc] init];
        _titleLabel.textColor   = UIColorFromRGB(0x333333, 1);
        _titleLabel.font        = [UIFont systemFontOfSize:15];
    }
    
    return _titleLabel;
}

- (UILabel *)subLabel {
    
    if (_subLabel == nil) {
        _subLabel = [[UILabel alloc] init];
        _subLabel.textColor        = UIColorFromRGB(0x333333, 1);
        _subLabel.font             = [UIFont systemFontOfSize:14];
        if ([[LZLanguageManager shareInstance] getLanguageType] == LanguageType_Ar){
            _subLabel.text = @"BHD/";
        } else {
            _subLabel.text = @"/BHD";
        }
        
    }
    
    return _subLabel;
}

- (UITextField *)textField {

    if (_textField == nil) {
        _textField = [[UITextField alloc] init];
        _textField.textColor    = UIColorFromRGB(0x333333, 1);
        _textField.tintColor    = UIColorFromRGB(0x33a928, 1);
        _textField.font         = [UIFont systemFontOfSize:14];
        _textField.keyboardType = UIKeyboardTypeDecimalPad;
        _textField.delegate = self;
        [_textField addTarget:self action:@selector(textFieldEditDidBegin:) forControlEvents:UIControlEventEditingDidBegin];
        [_textField addTarget:self action:@selector(textFieldEditDidEnd:) forControlEvents:UIControlEventEditingDidEnd];

    }
    
    return _textField;
}

- (UIView *)line {
    
    if (_line == nil) {
        _line = [[UIView alloc] init];
        _line.backgroundColor = UIColorFromRGB(0xeeeeee, 1);
    }
    
    return _line;
}

- (void)textFieldEditDidBegin:(UITextField *)textField {
    if ([LZAlertViewManager shareInstance].isShowAlert) {
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [textField resignFirstResponder];    
        });
    }
}

- (void)textFieldEditDidEnd:(UITextField *)textFild {
    if (self.textFieldEditDidEndBlock) {
        self.textFieldEditDidEndBlock(textFild.text, textFild.tag,self.indexPath);
    }
}

- (void)setCouponListModel:(LZCouponListModel *)couponListModel {

    _couponListModel = couponListModel;
    switch (self.textField.tag) {
        case 101:
        {
            
            self.titleLabel.text = LZLocalizedString(@"coupon_amount", @"");
            if (couponListModel.couponsMoney && couponListModel.couponsMoney > 0) {
                self.textField.text = [NSString stringWithFormat:@"%0.3f", couponListModel.couponsMoney]; //使用限制
            }
            else {
                self.textField.text = nil;
                self.textField.placeholder = @"???";//@"enter coupon amount";
            }
        }
            break;
        case 203:
        {
            self.titleLabel.text = LZLocalizedString(@"group_order_price", @"");
            if (couponListModel.grouponAfterCost && couponListModel.grouponAfterCost > 0 ) {
                self.textField.text = [NSString stringWithFormat:@"%0.3f", couponListModel.grouponAfterCost];
            }
            else {
                self.textField.text = nil;
                self.textField.placeholder = @"???";//@"enter group order price";
            }
        }
            break;
            
        default:
            break;
    }
}

- (void)setMerchantInfoModel:(LZMerchantInfoModel *)merchantInfoModel {
    
    self.titleLabel.text = LZLocalizedString(@"cost_person", nil);
    
    if (merchantInfoModel) {
        _merchantInfoModel = merchantInfoModel;
        self.textField.text = [NSString stringWithFormat:@"%0.3f", self.merchantInfoModel.averageConsumption];
    }
    else {
        
        self.textField.placeholder = @"???";
    }
}

- (void)setTextfieldTag:(NSInteger)tag {
    self.textField.tag = tag;
}

@end
